package com.example.experiment5;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class BluetoothActivity extends AppCompatActivity implements
        AdapterView.OnItemClickListener {
    private static final String TAG = "BluetoothActivity";

    // 本地蓝牙适配器
    private BluetoothAdapter bluetoothAdapter;
    private Button btn_search;
    private boolean scanning;
    private Handler handler;
    // Stops scanning after 10 seconds.
    private static final long SCAN_PERIOD = 5000;

    //搜索到蓝牙显示列表
    private ListView mylistview;
    // listview的adapter
    private ArrayAdapter<String> mylistAdapter;

    // 存储搜索到的蓝牙
    private List<String> bluetoothdeviceslist = new ArrayList<String>();

    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth);
        checkBluetoothPermission();

        initview();
        SearchBluetoot();
    }

    private void initview() {
        mylistview = (ListView) findViewById(R.id.mylistview);
        btn_search = (Button) findViewById(R.id.btn_search);

        // 获取本地蓝牙适配器
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setTitle("正在搜索...");
                // 判断是否在搜索,如果在搜索，就取消搜索
                if (bluetoothAdapter.isDiscovering()) {
                    bluetoothAdapter.cancelDiscovery();
                }
                // 开始搜索
                bluetoothAdapter.startDiscovery();
            }
        });

    }

    public void SearchBluetoot() {
        // 判断有没有蓝牙硬件支持
        if (bluetoothAdapter == null) {
            Log.d(TAG, "设备不支持蓝牙");
            Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show();
            finish();
        }
        else
        {
            Log.d(TAG, "找到蓝牙");
        }

        // 判断是否打开了蓝牙
        if (!bluetoothAdapter.isEnabled()) {
            Log.d(TAG, "没有打开蓝牙");
            // 我们通过startActivityForResult()方法发起的Intent将会在onActivityResult()
            // 回调方法中获取用户的选择，比如用户单击了Yes开启，
            // 那么将会收到RESULT_OK的结果，
            // 如果RESULT_CANCELED则代表用户不愿意开启蓝牙
            // 弹出对话框提示用户是否打开
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, 1);
            // 没有提示强制打开蓝牙(用enable()方法来开启，
            // 无需询问用户(实惠无声息的开启蓝牙设备),
            // 这时就需要用到android.permission.BLUETOOTH_ADMIN权限)
            // mBluetoothAdapter.enable();
        } else {
            // 获的已经配对的设备
            Set<BluetoothDevice> myDevices = bluetoothAdapter.getBondedDevices();
            /// 判断是否有配对过的设备
            if (myDevices.size() > 0) {
                for (BluetoothDevice device : myDevices) {
                    // 遍历到列表中
                    bluetoothdeviceslist.add(device.getName() + ":" + device.getAddress() + "\n");
                }
            }
            else
                Log.d(TAG, "没有找到配对的蓝牙设备");

            mylistAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    android.R.id.text1, bluetoothdeviceslist);
            mylistview.setAdapter(mylistAdapter);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                scanLeDevice(true);
            }

            // 找到设备的广播
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            // 注册广播
            registerReceiver(myreceiver, filter);
            // 搜索完成的广播
            filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            // 注册广播
            registerReceiver(myreceiver, filter);
        }
    }



    /**
     * 扫描蓝牙设备
     * @param enable 开始/停止扫描
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void scanLeDevice(final boolean enable) {
        Log.d(TAG, "扫描 LEBluetooth");

        handler = new Handler();
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    scanning = false;
                    bluetoothAdapter.stopLeScan(mLeScanCallback);
                    BluetoothActivity.this.invalidateOptionsMenu();
                }
            }, SCAN_PERIOD);

            scanning = true;
            bluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            scanning = false;
            bluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }

    // 设备扫描回调
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            Log.i(TAG, "蓝牙信息强度 rssi: " + rssi);
        }
    };

    // 广播接收器
    private final BroadcastReceiver myreceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 收到的广播类型
            String action = intent.getAction();
            // 发现设备的广播
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // 从intent中获取设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // 判断是否配对过
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    // 添加到列表
                    bluetoothdeviceslist.add(device.getName() + ":"+ device.getAddress() + "\n");
                    mylistAdapter.notifyDataSetChanged();
                }
                // 搜索完成
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                setTitle("搜索完成！");
            }
        }
    };

    /*
       校验蓝牙权限
    */
    private void checkBluetoothPermission() {
        if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) {
            // Android M Permission check
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[] {
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION
                        },
                        PERMISSION_REQUEST_COARSE_LOCATION);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (grantResults.length > 0 && grantResults[0] ==
                PackageManager.PERMISSION_GRANTED) {
            SearchBluetoot();
        } else { // 申请失败，可以继续向用户解释。
            Toast.makeText(BluetoothActivity.this, "没有权限，无法使用",
                    Toast.LENGTH_SHORT).show();
        }
        return;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
